<?php
/**
 * Created by PhpStorm.
 * User: 式神 (luck48.com)
 * Email: 289650682@qq.com
 * Name: ${NAME}Administrator
 * Date: 2017-04-01
 * Time: 11:06
 */
namespace App\Http\Controllers\Home;

use App\Http\Models\Article;
use App\Http\Models\Category;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Route;

class CategoryController extends CommonController
{
    //主页
    public function index(){
        $route=Route::Input('id');
        if($route){
            $cid=$route;
        }else{
            $cid=0;
        }
        $article=new Article();
        $cate=Category::all();
        $cids=[];
        $cids=getChildsId($cate,$cid);
        array_push($cids,$cid);
        if($cid!=0){
            $t=Category::where('id',$cid)->first()->template;
            $data=$article->whereIn('cid',$cids)->orderBy('id','desc')->get();
            $template=$t?$t:'list';
        }else{
            $data=$article->orderBy('id','desc')->get();
            $template='list';
        }
        foreach($data as $v){
            $v->url=url('').'/content/'.$v->id;
        }
        return view($this->template.'.'.$template,compact('cid','data'));
    }

}